package com.zt.proxy.util;

import java.security.MessageDigest;

import lombok.extern.slf4j.Slf4j;

/**
 * 
* @Title: MD5Util.java  
* @Description: MD5加密
* @author zhoutao    
* @date 2019年2月21日 上午11:05:13  
* @version V1.0
 */
@Slf4j
public class MD5Util {

    /** 
     * MD5 加密 
     * @param str
     */  
    public static String getMD5Str(String str) {  
        MessageDigest messageDigest = null;  
        try {
            messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.reset();  
            messageDigest.update(str.getBytes("UTF-8"));
        } catch (Exception e) {
            log.error("md5异常", e);
        }
        if(messageDigest==null){
            return null;
        }
        byte[] byteArray = messageDigest.digest();  
        StringBuilder md5StrBuff = new StringBuilder();  
        for (int i = 0; i < byteArray.length; i++) {              
            if (Integer.toHexString(0xFF & byteArray[i]).length() == 1){
                md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));  
            } else {
                md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));  
            }
        }
        log.info("md5Str{}", md5StrBuff.toString());
        return md5StrBuff.toString();  
    }
}
